package com.ziyia.a257;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private MyAdapter myAdapter;
    private List<Map> mapList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        mapList = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", "title - " + i);
            map.put("content", "content - " + i);
            mapList.add(map);
        }


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);

        recyclerView = findViewById(R.id.list);
        recyclerView.setLayoutManager(linearLayoutManager);

        myAdapter = new MyAdapter(this, mapList);


        recyclerView.setAdapter(myAdapter);




    }

    public void add(View view) {

        for (int i = 0; i < 10; i++) {
            Map map = new HashMap();
            map.put("title", "add");
            map.put("content", "add");
            mapList.add(map);
        }

        myAdapter.notifyItemRangeInserted(mapList.size()-1, 10);
        recyclerView.scrollToPosition(mapList.size() -1);
    }


    static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private Context context;
        private List<Map> mapList;


        public MyAdapter(Context context, List<Map> mapList) {
            this.context = context;
            this.mapList = mapList;
        }



        @NonNull
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item, null);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
            holder.title.setText(mapList.get(position).get("title").toString());
            holder.content.setText(mapList.get(position).get("content").toString());
        }

        @Override
        public int getItemCount() {
            return mapList.size();
        }


        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView title;
            TextView content;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title);
                content = itemView.findViewById(R.id.content);

            }
        }
    }
}